package com.youkeyi.ddy.cloud.common.wx.applet.message.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * WxTemplateMessageSendUtils
 *
 * @Author liuxiawang
 * @Date 5/4/23 6:12 PM
 * @Location shenzhen.china
 */
public class WxTemplateMessageSendUtils {

    public static class WeChatTemplateMsg {
        /**
         * 消息
         */
        private String value;
        /**
         * 消息颜色
         */
        private String color;

        public WeChatTemplateMsg() {
        }
        public WeChatTemplateMsg(String value) {
            this.value = value;
            // this.color = "#173177";
        }
        public WeChatTemplateMsg(String value, String color) {
            this.value = value;
            this.color = color;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
        public String getColor() {
            return color;
        }
        public void setColor(String color) {
            this.color = color;
        }
    }

    public static Boolean send(String templateId, String openId, Map<String, Object> sendMag) {
        // 去拿access_token
        String accessToken = WxTokenUtils.getAccessToken(WxTokenUtils.APPLET_APPID_DDY);
        if (StringUtils.isBlank(accessToken)) {
            System.err.println("AccessToken为空");
            return Boolean.FALSE;
        }

        // 发送消息接口
        String urlSendMessage = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken;

        RestTemplate restTemplate = new RestTemplate();
        //拼接base参数
        Map<String, Object> sendBody = new HashMap<>();
        sendBody.put("touser", openId);               // openId
        sendBody.put("data", sendMag);                // 模板参数
        sendBody.put("template_id", templateId);      // 模板Id
        // sendBody.put("url", "www.baidu.com");         // 点击模板信息跳转地址
        // sendBody.put("topcolor", "#FF00FF");          // 顶色
        ResponseEntity<String> forEntity = restTemplate.postForEntity(urlSendMessage, sendBody, String.class);
        System.err.println("结果是: " + JSON.toJSONString(forEntity));
        return Boolean.TRUE;
    }

    public static Boolean subscribeSend(String templateId, String openId, Map<String, Object> sendMag) {
        // 去拿access_token
        String accessToken = WxTokenUtils.getAccessToken(WxTokenUtils.APPLET_APPID_DDY);
        if (StringUtils.isBlank(accessToken)) {
            System.err.println("AccessToken为空");
            return Boolean.FALSE;
        }

        // 发送消息接口
        String urlSendMessage = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;

        RestTemplate restTemplate = new RestTemplate();
        //拼接base参数
        Map<String, Object> sendBody = new HashMap<>();
        sendBody.put("touser", openId);               // openId
        sendBody.put("data", sendMag);                // 模板参数
        sendBody.put("template_id", templateId);      // 模板Id
        // sendBody.put("page", "");                     // 非必填，跳转网页时填写
        // sendBody.put("miniprogram", "");              // 非必填，跳转小程序时填写，格式如{ "appid": "pagepath": { "value": any } }
        ResponseEntity<String> forEntity = restTemplate.postForEntity(urlSendMessage, sendBody, String.class);
        System.err.println("结果是: " + JSON.toJSONString(forEntity));
        return Boolean.TRUE;
    }









    public static void main(String[] args) {
        String appId = "wx0411fb6a82e1551d";
        String appIdSecret = "832bff3fb574a945026e564555310f35";
        String urlGetToken = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId +"&secret=" + appIdSecret;
        String res = HttpUtil.get(urlGetToken);
        JSONObject jsonObject = JSONObject.parseObject(res);
        String accessToken = jsonObject.getString("access_token");
        System.err.println("accessToken: " + accessToken);


        // openId代表一个唯一微信用户，即微信消息的接收人
        String openId = "ogMwE7WeJ3HX4ove7DHx837imoN4"; // unioid: o7WEJ6f0LXzLDVizncLxMRPwxOs4
        // 代办提醒
        String templateId = "dfSek_XfFWzbZbQVcyRZ-8lurWYTh9fd4EnHyGf-ERA";
        // 发送消息接口
        String urlSendMessage = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;

        // 模板参数
        Map<String, Object> sendMag = new HashMap<>();
        sendMag.put("thing1", new WxTemplateMessageSendUtils.WeChatTemplateMsg("你有一条新代办"));
        sendMag.put("thing14", new WxTemplateMessageSendUtils.WeChatTemplateMsg("你有一条新代办"));
        sendMag.put("phrase5", new WxTemplateMessageSendUtils.WeChatTemplateMsg("感谢使用"));
        sendMag.put("time19", new WxTemplateMessageSendUtils.WeChatTemplateMsg("2024-06-28"));
        RestTemplate restTemplate = new RestTemplate();
        //拼接base参数
        Map<String, Object> sendBody = new HashMap<>();
        sendBody.put("touser", openId);               // openId
        sendBody.put("url", "www.baidu.com");         // 点击模板信息跳转地址
        sendBody.put("topcolor", "#FF00FF");          // 顶色
        sendBody.put("data", sendMag);                // 模板参数
        sendBody.put("template_id", templateId);      // 模板Id
        ResponseEntity<String> forEntity = restTemplate.postForEntity(urlSendMessage, sendBody, String.class);
        System.err.println("结果是: " + JSON.toJSONString(forEntity));

    }

}
